CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate:
Passing a Variable?

 



gregarios
stranger

Mar 15, 2002, 12:18 PM

Post #1 of 11 (868 views)
Passing a Variable? Can't Post

How can I make this work? The variable in the
required printit.pl file doesn't contain anything.
It would end up printing "Today is" and thats it.

--------------------------------------------------
This is the contents of the main perl script file:
--------------------------------------------------
#!/usr/bin/perl5 -wT
use strict;
require '/usr/local/www/cgi-bin/printit.pl';

my $text = "friday!";
&print_text;
exit;

--------------------------------------------------
This is the contents of the printit.pl file:
--------------------------------------------------

sub print_text {
print "Content-type: text/html\n\n";
print "Today is $text";
}

1;

--------------------------------------------------

Greg J Piper
[url=http://www.macpicks.com]MacPiCkS



Paul
Enthusiast

Mar 15, 2002, 1:58 PM

Post #2 of 11 (866 views)
Re: [gregarios] Passing a Variable? [In reply to] Can't Post

Using a require for that purpose is really unnecessary.

Why not keep everything in one file:

print "Today is friday";

.....you'd probably want a way to detect the day automatically though to save you manually doing it every day.


(This post was edited by RedRum on Mar 15, 2002, 1:58 PM)


gregarios
stranger

Mar 15, 2002, 2:04 PM

Post #3 of 11 (863 views)
Re: [RedRum] Passing a Variable? [In reply to] Can't Post

This is not the actual code. The actual code is much more involved, and I need to store the subroutine in a different file so other scripts can use it. This is just a hypothetical scenario.

Pretend that I have multiple scripts and they all need to print "Today is whatever" depending on what script is calling it. :-)

Greg J Piper
[url=http://www.macpicks.com]MacPiCkS



dave
Novice

Mar 15, 2002, 2:58 PM

Post #4 of 11 (860 views)
Re: [gregarios] Passing a Variable? [In reply to] Can't Post

If you're using -w then you should have seen you're trying to use an uninitialzed value. ie: $text in
printit.pl needs a value.
Initialize $text in your print_text routine; ie: my $text = $_[0]; - or - my $text = shift; - and
pass the variable when you call print_text; ie: &print_text( $text );

You might like to have a think about writing modules instead of those old fashioned libraries.

dave


gregarios
stranger

Mar 15, 2002, 3:17 PM

Post #5 of 11 (855 views)
Re: [dave] Passing a Variable? [In reply to] Can't Post

Thanks for your help. Smile

I've been trying to figure out the syntax and such of doing modules. Can you show me how to do this example in the form of a module instead? (hypothetically)

Greg J Piper
[url=http://www.macpicks.com]MacPiCkS



dave
Novice

Mar 15, 2002, 4:33 PM

Post #6 of 11 (854 views)
Re: [gregarios] Passing a Variable? [In reply to] Can't Post

This will have to be really brief because I'm rather snowed under lately.
This is pass_a_var.cgi ...


Code
    

use strict; # Enforce private scoping of variables.
use lib 'c:/usr/etc/htdocs/karisma/cgi-bin/lib'; # The directory where you keep your modules.
use Printit; # Replaces require
my $p = Printit->new(); # Store a reference to a Printit object (anonymous hash).

my $text = 'Friday';
$p->printit( text => $text ); # Call printit and pass a named parameter with $text as the value.



This is Printit.pm ...


Code
    

package Printit; # package all the following functions and variables into the Printit namespace.
use strict;

sub new{ # The new method is the class constructor.
my( $class, %params ) = @_;
bless { %params }, $class; # Bless a reference to an anonymous hash into the Printit class. The address that this reference points to is the object of our endeavours.
}

sub printit{
my( $class, %params ) = @_;
print "Today is $params{'text'}\n";
$class->new( %params ); # Hand a hash which holds the goods off to the new method for blessing. (Actually, you don't need this if you're printing from this method).
}

1;



This is a very simple example. It uses a hash because I think they're the most
flexible, and are easier to read as your program grows. Learning Perl and
Programming Perl are two excellent books which cover this subject.
I should also mention that values are usually returned to your program; ie:

my $val = $p->printit( text => $text );
print $val->{'ret_val'};

...and in your printit method...

$params{'ret_val'} = "Today is $params{'text'}\n"

...instead of the print statement.
dave


(This post was edited by dave on Mar 15, 2002, 4:52 PM)


gregarios
stranger

Mar 16, 2002, 1:29 AM

Post #7 of 11 (842 views)
Re: [dave] Passing a Variable? [In reply to] Can't Post

Thank you. You've been more than helpful. Looks like I have a bunch of studying to do now. CrazySmile

Greg J Piper
[url=http://www.macpicks.com]MacPiCkS



yapp
User

Mar 16, 2002, 3:22 AM

Post #8 of 11 (839 views)
Re: [gregarios] Passing a Variable? [In reply to] Can't Post

If you want to use variables in other "required" files of your program aswell, use the vars pragma. use strict doesn't allow you just using a variable. You'll have to tell where it is stored. When using my(), the variable doesn't get stored in the symbol table, which is efficient. However, that automatically makes the variable unusable through your entire program.

[perl]
#!/usr/bin/perl -wT

use strict;
use vars qw($text);

require 'that_file.pl';
print "Today is $text\n";
[/perl]

that_file.pl:
[perl]
# no strict here.
$text = 'friday';
[/perl]

Yet Another Perl Programmer

_________________________________
~~> [url=http://www.codingdomain.com]www.codingdomain.com <~~
More then 3500 X-Forum [url=http://www.codingdomain.com/cgi-perl/downloads/x-forum]Downloads! Cool


Paul
Enthusiast

Mar 16, 2002, 3:42 AM

Post #9 of 11 (837 views)
Re: [yapp] Passing a Variable? [In reply to] Can't Post

>>
that_file.pl:

# no strict here.
$text = 'friday';
<<

You'd need a true value remember :)


gregarios
stranger

Mar 16, 2002, 11:02 AM

Post #10 of 11 (826 views)
Re: [RedRum] Passing a Variable? [In reply to] Can't Post

Ah! I like that method. Is it any less secure to "use vars" than to do it the first way or making a .pm? I could really use this for a couple of my programs where I just need to have a remote file that has a list of set variables in it.
(and a 1; as well Wink)

Greg J Piper
[url=http://www.macpicks.com]MacPiCkS



yapp
User

Mar 17, 2002, 6:43 AM

Post #11 of 11 (816 views)
Re: [gregarios] Passing a Variable? [In reply to] Can't Post

It just depends on what you want to do with your program. At least, you have the settings separated from the programming code, which is a good thing.

For a simple program, my use vars method would be good enough. It just creates new symbol entries in the package namespace, which can be used globally in that package (often main)

However, when you're using 50 variables, you might want to use a different approach. That also requires good development. I don't know any trick that just solves such bug problem.

The .pm method actually does some Object Orientated programming trick. OOp is a very very very cool way of programming, if you're deep in it and understand the use of it.. Otherwise, I believe it's just confusing....althrough some people think different about this.

Yet Another Perl Programmer

_________________________________
~~> [url=http://www.codingdomain.com]www.codingdomain.com <~~
More then 3500 X-Forum [url=http://www.codingdomain.com/cgi-perl/downloads/x-forum]Downloads! Cool

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives