use strict and local variables
Dear Users is there a possibility to use use strict and to define variables in the programm, which are also seen in the subroutines called from this block? I want the same value in the var in the subroutine like before, but without it to define as global. What could I do? Thanks for all ideas and help. Greetings Raphael eg: use strict; my $var = 20; print $var\n; routine; exit; sub routine { print $var\n; } -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: use strict and local variables
eg: use strict; my $var = 20; print $var\n; routine; exit; sub routine { print $var\n; } Hi,you can do it by passing the vars to the subroutine like: my $var = 20; routine($var); sub routine { my $var = shift; print $var; } -- Books below translated by me to Chinese. Practical mod_perl: http://home.earthlink.net/~pangj/mod_perl/ Squid the Definitive Guide: http://home.earthlink.net/~pangj/squid/ -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: use strict and local variables
--- Jeff Pang [EMAIL PROTECTED] wrote: Hi,you can do it by passing the vars to the subroutine like: my $var = 20; routine($var); sub routine { my $var = shift; print $var; } I'll second this recommendation because it makes the subroutines more flexible (what if they want a different variable in the future?). The only change I would make is to strip the leading ampersand on the sub call: routine($var); Using a leading ampersand leads to very strange bugs if you call a subroutine with a leading ampersand and no parentheses because the current value of @_ is then passed to the calling subroutine. It's then very easy, when refactoring, to use subname instead of subname(). Avoid that ampersand unless you know exactly why you're using it :) Cheers, Ovid -- Buy the book -- http://www.oreilly.com/catalog/perlhks/ Perl and CGI -- http://users.easystreet.com/ovid/cgi_course/ -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: use strict and local variables
Thanks for your answer! But, my problem is, i must see this variable after the call of the sub. I'm sorry for the first example, it was inaccurate. But this is ok (I think) :) (because I have a lot of variables, which I must change in the sub, I want to define they as global inside my parent-routine (in the example: the programm, but by me: the parent-sub)). Thanks for all, Raphael eg: use strict; my $var = 20; print before: $var\n; routine; print after: $var\n; exit; sub routine { $var += 1; } On Mon, Oct 16, 2006 at 07:36:13PM +0800, Jeff Pang wrote: eg: use strict; my $var = 20; print $var\n; routine; exit; sub routine { print $var\n; } Hi,you can do it by passing the vars to the subroutine like: my $var = 20; routine($var); sub routine { my $var = shift; print $var; } -- Books below translated by me to Chinese. Practical mod_perl: http://home.earthlink.net/~pangj/mod_perl/ Squid the Definitive Guide: http://home.earthlink.net/~pangj/squid/ -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: use strict and local variables
* Raphael Brunner [EMAIL PROTECTED] [2006-10-16T08:38:00] But, my problem is, i must see this variable after the call of the sub. I'm sorry for the first example, it was inaccurate. But this is ok (I think) :) (because I have a lot of variables, which I must change in the sub, I want to define they as global inside my parent-routine (in the example: the programm, but by me: the parent-sub)). So, pass in a reference to it. Instead of: my $var = 20; print before: $var\n; routine; print after: $var\n; exit; sub routine { $var += 1; } Write: my $var = 20; print before: $var\n; routine(\$var); print after: $var\n; sub routine { my ($input_ref) = @_; $$input_ref += 1; } Consult perldoc perlreftut for more. -- rjbs -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: use strict and local variables
eg: use strict; my $var = 20; print before: $var\n; routine; print after: $var\n; exit; sub routine { $var += 1; } Hi,you don't need the global vars at all.The same way,you can write it like: use strict; my $var = 20; print before: $var\n; $var = routine($var); print after: $var\n; sub routine { my $var = shift; ++$var; } __END__ Hope it helps. -- Books below translated by me to Chinese. Practical mod_perl: http://home.earthlink.net/~pangj/mod_perl/ Squid the Definitive Guide: http://home.earthlink.net/~pangj/squid/ -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: use strict and local variables
Raphael Brunner wrote: On Mon, Oct 16, 2006 at 07:36:13PM +0800, Jeff Pang wrote: Raphael Brunner wrote: eg: use strict; my $var = 20; print $var\n; routine; exit; sub routine { print $var\n; } Hi,you can do it by passing the vars to the subroutine like: my $var = 20; routine($var); sub routine { my $var = shift; print $var; } Thanks for your answer! But, my problem is, i must see this variable after the call of the sub. I'm sorry for the first example, it was inaccurate. But this is ok (I think) :) (because I have a lot of variables, which I must change in the sub, I want to define they as global inside my parent-routine (in the example: the programm, but by me: the parent-sub)). Thanks for all, Raphael eg: use strict; my $var = 20; print before: $var\n; routine; print after: $var\n; exit; sub routine { $var += 1; } (Please bottom-post your replies so that the thread remains comprehensible. Thanks.) I don't understand what problem you're having Raphael. Both of the examples that you've posted should do what you say you want. What's wrong with those? You've been given a lot of useful advice which you should take heed of, in particular that you shouldn't use the ampersand when calling a subroutine. There's also no need for the exit call in your program as there is nothing executable after it. I would add that you can write a subroutine that modifies its actual parameters without passing them by reference if you adjust the @_ parameter array directly, as in the code below. There's nothing wrong with modifying global variables like this as long as it's done clerly and coherently and in moderation. HTH, Rob use strict; use warnings; my $var = 20; print before: $var\n; increment($var); print after: $var\n; sub increment { $_[0]++; } **OUTPUT** before: 20 after: 21 -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Global and Local variables issue
Ok what I have is 3 variables assigned in the global block using my then when I try to use those variables later in a subroutine I get an error. The error is below: Error during compilation of /usr/local/rt3/local/html/cgi-bin/aupsearch.cgi: Variable $ip will not stay shared at /usr/local/rt3/local/html/cgi-bin/aupsearch.cgi line 141. Variable $date will not stay shared at /usr/local/rt3/local/html/cgi-bin/aupsearch.cgi line 142. Variable $customer will not stay shared at /usr/local/rt3/local/html/cgi-bin/aupsearch.cgi line 158. Now it was my understanding that if I used my in the global block, that I could reference that variable in a subroutine. OH, and strict is on, and cannot be taken off. Any help with this will be greatly appreciated. Actual Code below, some code omitted because of security and for clarity. . # Get parameters from the html form my $date = $q-param('date'); my $ip = $q-param('ip'); my $customer = $q-param('customer'); ### Sub to Print VZD Table ### sub PrintVZDTable { my @row; my $rsltid; my $rsltdate; my $rslttime; my $rsltrecord_type; my $rsltfullname; my $rsltframed_ip_address; my $tableline; my $rsltuser_name; my $rsltrecord_time; my $rsltevent_timestamp; my $sth; print $ip; print $date; .. } Chris Hood Investigator Verizon Global Security Operations Center Email: mailto:[EMAIL PROTECTED] [EMAIL PROTECTED] Desk: 972.399.5900 Verizon Proprietary NOTICE - This message and any attached files may contain information that is confidential and/or subject of legal privilege intended only for the use by the intended recipient. If you are not the intended recipient or the person responsible for delivering the message to the intended recipient, be advised that you have received this message in error and that any dissemination, copying or use of this message or attachment is strictly forbidden, as is the disclosure of the information therein. If you have received this message in error please notify the sender immediately and delete the message.
Re: Global and Local variables issue
Christopher L Hood wrote: Ok what I have is 3 variables assigned in the global block using my then when I try to use those variables later in a subroutine I get an error. The error is below: Error during compilation of /usr/local/rt3/local/html/cgi-bin/aupsearch.cgi: Variable $ip will not stay shared at /usr/local/rt3/local/html/cgi-bin/aupsearch.cgi line 141. Variable $date will not stay shared at /usr/local/rt3/local/html/cgi-bin/aupsearch.cgi line 142. Variable $customer will not stay shared at /usr/local/rt3/local/html/cgi-bin/aupsearch.cgi line 158. Now it was my understanding that if I used my in the global block, that I could reference that variable in a subroutine. OH, and strict is on, and cannot be taken off. First, you should look up the message in perldoc perldiag. Are you possibly running the program under mod_perl? I'm asking because mod_perl may trigger that warning even without nested subroutines. One possible solution is to pass the variables as arguments when calling the subroutine, instead of referring to them from inside the sub. HTH -- Gunnar Hjalmarsson Email: http://www.gunnar.cc/cgi-bin/contact.pl -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Module help (local variables in seperate directory)
I am having trouble creating a modules which splits its functions and variables between two files in two separate directories. I want to have a set of global functions (i.e. open database connection...) which would live in the main Perl path, and a local configuration file (containing Host name, user name, password...) which would live in the user's local directory. When the module was called, it would instantiate with the local variables. Here is what I have tried: GLOBAL FUNCTIONS in: /usr/lib/perl5/5.8.0/MetaManageGlobal.pm: package MetaManagerGlobal; require Exporter; our ($VERSION, $dbh); our @ISA = qw(Exporter); our @EXPORT = qw(connectDb $dbh); $VERSION = 1.0.0; # Content Database connect sub connectDb { require DBI; $dbh = DBI-connect(dbi:mysql:$cfg-{dbName}:$cfg-{dbServer}, $cfg-{ dbUser}, $cfg-{dbPassword}) || Die $DBI::errstr; } +++ LOCAL VARIALBES in: /home/httpd/html/admin/MetaManager.pm: package MetaManager; require Exporter; require MetaManagerGlobal; our ($VERSION, $cfg, $dbh); our @ISA = qw(Exporter); our @EXPORT = qw(connectDb $dbh $cfg); $VERSION = 1.0.0; $cfg-{dbServer} = Host Name; # Database server host $cfg-{dbName} = Database Name; # Database name $cfg-{dbUser} = User Name; # Database user $cfg-{dbPassword} = Password; # Database password +++ From the page (which uses Apache::ASP) I want to be able to simply call the function: % use MetaManager; ConnectDb(); % And have my database handle opened to whatever specifications exist in the local MetaManager.pm module. When I try this, the ConnectDb function is found, but no Db is opened, suggesting to me that the variables are not being properly scoped or imported. Many thanks for assistance. Andrew Koebrick Web Coordinator / Librarian Dept. of Administration State of Minnesota 658 Cedar St. St. Paul, MN 55155 651-296-4156 http://server.admin.state.mn.us -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
RE: Module help (local variables in seperate directory)
Andrew Koebrick wrote: I am having trouble creating a modules which splits its functions and variables between two files in two separate directories. I want to have a set of global functions (i.e. open database connection...) which would live in the main Perl path, and a local configuration file (containing Host name, user name, password...) which would live in the user's local directory. When the module was called, it would instantiate with the local variables. Here is what I have tried: GLOBAL FUNCTIONS in: /usr/lib/perl5/5.8.0/MetaManageGlobal.pm: package MetaManagerGlobal; require Exporter; our ($VERSION, $dbh); our @ISA = qw(Exporter); our @EXPORT = qw(connectDb $dbh); $VERSION = 1.0.0; # Content Database connect sub connectDb { require DBI; $dbh = DBI-connect(dbi:mysql:$cfg-{dbName}:$cfg-{dbServer}, $cfg-{ dbUser}, $cfg-{dbPassword}) || Die $DBI::errstr; } +++ LOCAL VARIALBES in: /home/httpd/html/admin/MetaManager.pm: package MetaManager; require Exporter; require MetaManagerGlobal; our ($VERSION, $cfg, $dbh); our @ISA = qw(Exporter); our @EXPORT = qw(connectDb $dbh $cfg); $VERSION = 1.0.0; $cfg-{dbServer} = Host Name; # Database server host $cfg-{dbName} = Database Name; # Database name $cfg-{dbUser} = User Name; # Database user $cfg-{dbPassword} = Password; # Database password +++ From the page (which uses Apache::ASP) I want to be able to simply call the function: % use MetaManager; ConnectDb(); % And have my database handle opened to whatever specifications exist in the local MetaManager.pm module. When I try this, the ConnectDb function is found, but no Db is opened, suggesting to me that the variables are not being properly scoped or imported. MetaManager is exporting $cfg, but because MetaManagerGlobal doesn't use MetaManager (for obvious reasons), MetaManagerGlobal insn't importing $cfg. Suggestions: 1) add use strict to each module. This would highlight the problem more clearly. 2) have MetaManagerGlobal export $cfg, and then use (not require) MetaManagerGlobal from MetaManager. This will allow your local module to set the global module's values. If you have other modules that need to access $cfg, have them use MetaManagerGlobal also. -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Local variables
Hi! I have a Pretty class, with 15 methods (or more). I was reading a Perl Tutorial and read that I could create local variables. So In many of my methods Im using variables, but Im not declaring them like 'local $var', just like 'my $var'. So I changed a few variables from 'my' to 'local', and When I tried to see if it was working I got: [EMAIL PROTECTED] Perl]$ perl Persona.pm Global symbol $name requires explicit package name at Persona.pm line 22. Global symbol $name requires explicit package name at Persona.pm line 24. Global symbol $name requires explicit package name at Persona.pm line 25. Global symbol $name requires explicit package name at Persona.pm line 26. Execution of Persona.pm aborted due to compilation errors. The method that I modified: sub hablar { my $person = shift; local $name; print Give me your second name.. ; $name = STDIN; chop $name; print Your second name is $name\n; if ($_[0] eq fuerte) { print My first name is $person-{NOMBRE}!!!\n; } elsif($_[0] eq bajo) { print My alias it $person-{NOMBRE}\n; } } And obviously, 'local $name', was 'my $name' and it was working so nice.. So is it necesary to declare the variables as local vars? Cause Im getting errors :( THankss! Pablo -- Pablo Fischer Sandoval ([EMAIL PROTECTED]) http://www.pablo.com.mx http://www.debianmexico.org GPG FingerTip: 3D49 4CB8 8951 F2CA 8131 AF7C D1B9 1FB9 6B11 810C Firma URL: http://www.pablo.com.mx/firmagpg.txt -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: Local variables
On Sun, Jul 27, 2003 at 11:59:59AM +, Pablo Fischer wrote: I have a Pretty class, with 15 methods (or more). I was reading a Perl Tutorial and read that I could create local variables. So In many of my methods Im using variables, but Im not declaring them like 'local $var', just like 'my $var'. [ snip ] And obviously, 'local $name', was 'my $name' and it was working so nice.. So is it necesary to declare the variables as local vars? Not at all. :-) Have a look at perlsub -- the sections called Private Variables with my() and Temporary Variables with local() are the official description of the difference between these two. There's also a very good article on scoping here: http://perl.plover.com/FAQs/Namespaces.html -- Steve -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: Local variables
Pablo Fischer wrote at Sun, 27 Jul 2003 11:59:59 +: I have a Pretty class, with 15 methods (or more). I was reading a Perl Tutorial and read that I could create local variables. So In many of my methods Im using variables, but Im not declaring them like 'local $var', just like 'my $var'. So I changed a few variables from 'my' to 'local', and When I tried to see if it was working I got: [EMAIL PROTECTED] Perl]$ perl Persona.pm Global symbol $name requires explicit package name at Persona.pm line 22. Global symbol $name requires explicit package name at Persona.pm line 24. Global symbol $name requires explicit package name at Persona.pm line 25. Global symbol $name requires explicit package name at Persona.pm line 26. Execution of Persona.pm aborted due to compilation errors. Usually you will only use my and our variable declarations. my declares a variable that is scoped to the current lexical block. Allthough that might sound hard at the first time, it's just the usual variable declaration for non-global variables, like in your subroutines. With our you declare global (package scobed variables). With local you declare a localiced variation of a _global_ variable. That's not that frequent used, as you first need a global variable and then a reason why to overwrite it and finally why only at the current lexical block :-) Especially it's usually sensless to use it for subroutine arguments. Some useful and standard moments to use it are e.g. local @ARGV = *.txt; while () { # ... } local $/ = undef; my $whole_text_of_a_file = FILE; local $ENV{CLASSPATH} = path/to/a/some/java/excecutable/files; `java some.boring.java.program In the first snippet, you change the global @ARGV holding the arguments passed to the script, just to use the nice power of the operator. It would be boring to write foreach my $file (*.txt) { open FILE, $file or die ...; while (FILE) { # ... } } In the second snippet you change the global line separator to slurp whole a file in and in the third, you change an environment variable so that you can e.g. call an evil external program with a special setting. All these cases (and most of the other useful cases for local in use) have in comman, that there is a powerful use of a global variable that is so nice that it would be foolish to use non-global or different variables. Of course, you could simulate the behaviour of every local with my $old_value= $global_variable; $global_variable = $local_value; # ... $global_variable = $old_value; But as it is very boring and errorprone (and perhaps more threadsafe), the local statement is used instead. For all the rest of information, please read the already recommended perldoc perlsub Greetings, Janek -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: Local variables
El día Sunday 27 July 2003 5:43 a Steve Grazzini mandó el siguiente correo: Have a look at perlsub -- the sections called Private Variables with my() and Temporary Variables with local() are the official description of the difference between these two. There's also a very good article on scoping here: http://perl.plover.com/FAQs/Namespaces.html Thanks for the link! Now I know when do I need to use local and my, hehe From the website: When to Use my and When to Use local Always use my; never use local. Wasn't that easy? funny XD. No, really, now I understand. thanks Steve! Pablo -- Pablo Fischer Sandoval ([EMAIL PROTECTED]) http://www.pablo.com.mx http://www.debianmexico.org GPG FingerTip: 3D49 4CB8 8951 F2CA 8131 AF7C D1B9 1FB9 6B11 810C Firma URL: http://www.pablo.com.mx/firmagpg.txt -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: my local variables
On Sunday, May 19, 2002, at 10:56 , Postman Pat wrote: Greetings, I read in the book SAMS Teach Yourself Perl in 21 Days that you can use my/local to declare vars. They explanation I got from the book did not do much explaining on exactly what the difference is between the two. Can someone please shed some light. you clearly want to follow Sudarsan Raghavan [EMAIL PROTECTED] advice and scope out http://perl.plover.com/FAQs/Namespaces.html. A must read. the short game is #!/usr/bin/perl -w use strict; my $var=1; sub thisSub { local( $var ); $var++; print the var is $var\n; } print we have var as $var before the call\n thisSub(); print we have var as $var after the call\n for fun try that without the 'my'. ciao drieux --- -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: my local variables
#!/usr/bin/perl -w use strict; my $var=1; sub thisSub { local( $var ); This will give this error Can't localize lexical variable ... A working example #!/usr/bin/perl -w use strict; $main::var=1; sub print_var { print package var is $main::var\n;} sub thisSub { local $main::var = 1; $main::var++; print the modified var in thisSub $main::var\n; print_var (); } print_var (); thisSub(); print_var (); You can also take a look at this script http://www.crusoe.net/~jeffp/docs/my_vs_local -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
my local variables
Greetings, I read in the book SAMS Teach Yourself Perl in 21 Days that you can use my/local to declare vars. They explanation I got from the book did not do much explaining on exactly what the difference is between the two. Can someone please shed some light. Regards LK -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: my local variables
http://perl.plover.com/FAQs/Namespaces.html Postman Pat wrote: Greetings, I read in the book SAMS Teach Yourself Perl in 21 Days that you can use my/local to declare vars. They explanation I got from the book did not do much explaining on exactly what the difference is between the two. Can someone please shed some light. Regards LK -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]