473,320 Members | 2,092 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

what is scope of $1 or why can't I call the same sub twice?

BeemerBiker
This should have worked. It only printed 11111 and did not print the 22222. So am I missing a "delete $1" or a "free $1" before the routine returns? Googleing was not helpful.

Expand|Select|Wrap|Line Numbers
  1. use strict;
  2. use warnings;
  3.  
  4. sub printit {
  5.  my ($c) = @_;
  6.  
  7. print "arg:" . $c . "\n";
  8.  
  9.  $c=~ ?<nonce>(.*)</nonce>?;
  10.  
  11.  print "between the nonce: " . $1 . " \n";
  12.  
  13. }
  14.  
  15. my $a = "<nonce>11111</nonce>";
  16. my $b = "<nonce>22222</nonce>";
  17.  
  18. printit $a;
  19. printit $b;
=========results below=====
Expand|Select|Wrap|Line Numbers
  1. C:\perl>test99.pl
  2. arg:<nonce>11111</nonce>
  3. between the nonce: 11111
  4. arg:<nonce>22222</nonce>
  5. Use of uninitialized value $1 in concatenation (.) or string at C:\perl\test99.pl line 12.
  6. between the nonce:
  7.  
;
May 30 '10 #1

✓ answered by numberwhun

Ok, I did some playing with this one and here is my revised version of your script, which works as you expect:

Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl
  2.  
  3.     use strict;
  4.     use warnings;
  5.  
  6.     sub printit {
  7.          my ($c) = shift @_;
  8.  
  9.          print "arg:" . $c . "\n";
  10.  
  11.          $c =~ m/<nonce>(.*)<\/nonce>/;
  12.  
  13.          print "between the nonce: " . $1 . " \n";
  14.  
  15. }
  16.  
  17. my $a = "<nonce>11111</nonce>";
  18. my $b = "<nonce>22222</nonce>";
  19.  
  20. &printit($a);
  21. &printit($b);
  22.  
You will notice the regex right off and that I have changed it. First, you really shouldn't use characters for the opening and closing of the regex that have special meaning to a regex. The "?" are such characters. You could use something like & even, but not something reserved for regex's. Also, when you use a different character than the standard / to start and end a regex, you need to put the m before the regex to precede the change. Its common practice.

Another thing, inside the regex, you need to put a backslash before the forward slash, or it won't work either.

I also touched up your coding for neatness and my own anality.

Also, to answer your question... there is no way to clear the variables of the regex out other than to have the function/subroutine end. When it does, they clear naturally.

Regards,

Jeff

3 1720
numberwhun
3,509 Expert Mod 2GB
Ok, I did some playing with this one and here is my revised version of your script, which works as you expect:

Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl
  2.  
  3.     use strict;
  4.     use warnings;
  5.  
  6.     sub printit {
  7.          my ($c) = shift @_;
  8.  
  9.          print "arg:" . $c . "\n";
  10.  
  11.          $c =~ m/<nonce>(.*)<\/nonce>/;
  12.  
  13.          print "between the nonce: " . $1 . " \n";
  14.  
  15. }
  16.  
  17. my $a = "<nonce>11111</nonce>";
  18. my $b = "<nonce>22222</nonce>";
  19.  
  20. &printit($a);
  21. &printit($b);
  22.  
You will notice the regex right off and that I have changed it. First, you really shouldn't use characters for the opening and closing of the regex that have special meaning to a regex. The "?" are such characters. You could use something like & even, but not something reserved for regex's. Also, when you use a different character than the standard / to start and end a regex, you need to put the m before the regex to precede the change. Its common practice.

Another thing, inside the regex, you need to put a backslash before the forward slash, or it won't work either.

I also touched up your coding for neatness and my own anality.

Also, to answer your question... there is no way to clear the variables of the regex out other than to have the function/subroutine end. When it does, they clear naturally.

Regards,

Jeff
May 31 '10 #2
Thanks Jeff - I would not have spotted the problem with the regex, especially since it worked on the first call to the subroutine.

I also tried the following which got rid of the $1 but still had the same bad behavior:
Expand|Select|Wrap|Line Numbers
  1. my ($uno) = $c=~ ?<nonce>(.*)</nonce>?;
  2. print "between the nonce: " . $uno . " \n";
  3.  
I am thinking that the fact it worked on the first call but not the second is more likely a bug than a feature of ActivePerl 5.10.1.

The actual text I was trying to parse was quote
"<boinc_gui_rpc_reply>
<nonce>1275311433.448608</nonce>
</boinc_gui_rpc_reply>"

I thought I had to have the ? to match the extra stuff before and after the <nonce> newlines
May 31 '10 #3
numberwhun
3,509 Expert Mod 2GB
Well, I am not using ActivePerl, I am using Perl itself as I am on Linux. Not sure why it works the first time through, but it does, and that seems to be just how it was evaluated. I would certainly avoid using regex characters as the regex begin/end characters. Use something safer. Some people prefer to use the % sign instead. Its up to you.

Regards,

Jeff
May 31 '10 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

7
by: QQ | last post by:
I have a function defined in CC file like int D(char* p, char* e, char* c,char* g1, short g1p, char* g2, short g2p, bool s ); I put it in the header file as int D(char* p, char* e, char* c,...
1
by: zx.zhangxiong | last post by:
Dear all, I'm puzzled about the usage of class member function. Any help would be appreciated. class Account { ...
0
by: Michał Januszczyk | last post by:
is it possible to define schema for the following element ? : <processor><param>arial</param><param>3.5</param><param>50</param><param>95</param><param>some text</param></processor I want to...
10
by: shailashri_sk | last post by:
Hello All, Just wanted to know, if there is IDE which provides compilers for diff languages like C, ADA, C++ etc. Can v call the procedures written in C++ or ADA, from these C prgms may be, by...
2
by: Newbie | last post by:
how can i call an oracle function? given a project_no, i need to call the function: ops$sqltime.pa_new_job_no_fn which will return the next job_no thanks in advance.
3
by: evelyne0510 | last post by:
Hi all, I have created a XML-RPC model (with server and client) written in Java. I want to call the methods in another XML-RPC model written in Python. I know that in Java, I can use like...
0
by: ahcirlko | last post by:
I am using different user controls in same page. I have a function inside of the .ascx file. How can I call it from another .ascx file Language C#
1
by: mamidogad | last post by:
Dear Sir; How can I call mandatory part of XML from other path and acheive validation process at the same time. My full XML is divided to three parts (front, body, and back) how can I call...
0
by: MuhammadUsman | last post by:
Hi I am writing a simple client server program in c# using sockets over UDP. Can I use same port for both the client and server to send and receive data (like in TCP) . I am trying to do the...
2
by: amievil | last post by:
wow... I think I'm in a trouble now. My boss is a web programmer. She said, the client asked her to develop a " dot net C" application. So, she bought Microsoft visualstudio .NET 2003 and...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.