Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw

Question regarding perl subroutine

by sureshsmr (Initiate)
on Jul 15, 2012 at 20:27 UTC ( #981922=perlquestion: print w/replies, xml ) Need Help??
sureshsmr has asked for the wisdom of the Perl Monks concerning the following question:

Dear Monks,

I need some advise to fix the error I am getting while running a perl code with a subprogram. Here is the perl sub program :

sub CREATE_GLOBAL_PTF_VIA_PTFMGR { my $pnum = (@_[0]); ##part number passed arg @_ into local var p +num my $coreid = (@_[1]); #use XML::Simple; #use HTTP::Response; #use LWP::UserAgent; my $ptfmgr_server= ""; ############## Call to PTF web services for description informatio +n ############# my $url = "http://$ptfmgr_server/services/GenerateGlobalPtf?coreid +=$coreid&partNumber=$pnum"; my $ua= new LWP::UserAgent; #my $ua->agent("Mozilla/5.0"); my $ua->agent(""); my $request = new HTTP::Request GET => "$url"; my $response = $ua->request($request); my $ptf_filename; my $log = "" if ( $response->is_success ) { my $content= $response->content; #print "\$content = $content\n"; ############## Read XML information ############# my $xml = new XML::Simple(suppressempty => ''); ### create obj +ect suppressempty returns empty elements as strings. my $data = $xml->XMLin($content); ### read XML file my $return_value = $data->{ReturnValue}; my $ptfmgr_error = $data->{Error}; my $ptf_filename = $data->{PtfFileName}; #print "\$return_value = $return_value\n"; #if ( $return_value == "1" ) { # my $log = "\sGlobal PTF generation to \"$ptf_filename\" w +as successful.\n"; #} else { # my $log = "\sPTFMGR returned the error:\n\n$ptfmgr_erro +r\n\nGlobal PTF not created for $ptf_filename.\n."; #} #} else { # my $log = "\sCould not create PTF via PTFMGR! It may be o +ffline.\n"; # } } return $ptf_filename; }

This is the error I get while running it:

/usr/local/bin/perl syntax error at line 164, near ") {" Global symbol "$ptf_filename" requires explicit package name at PtfSap line 184. syntax error at line 185, near "}" Execution of aborted due to compilation errors. v02k603:/edatools/served/pcs/ptfwes>

The syntax looks ok to me. May be the error is due to something else. I am running this on a unix server. Look forward to your advice. Thanks in advance, Yours Suresh

Replies are listed 'Best First'.
Re: Question regarding perl subroutine
by davies (Parson) on Jul 15, 2012 at 20:38 UTC

      Hi John Davies, Thanks a lot! That was good catch. Now I am getting a different error as below:

      /usr/local/bin/perl Can't call method "agent" on an undefined value at + line 158.

      Here is the code on line 158:

      my $ua->agent("Mozilla/5.0"); #my $ua->agent("");

      is the syntax for calling "agent" method ok? I am not sure what tool is on our unix server. Any suggestions ? Thanks Suresh

        $ua is not what you think. It is undefined, probably because the code above lacks error checks. See Basic debugging checklist.


        Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)

        No, that syntax cannot be right. Looking at the code you posted originally, I see:

        my $ua= new LWP::UserAgent; #my $ua->agent("Mozilla/5.0"); my $ua->agent("");

        You seem to be trying to use strict;, which is good (see my home node), but without understanding what it does. PerlIntro ( has a section on variable scoping that it might help you to understand. my creates a new instance of a variable. You may have seen examples of messages telling you that a declaration masks a previous declaration. This is telling you that you have re-initialised a variable name, which is unlikely to be what you want. I think what you want is something like:

        my $ua= new LWP::UserAgent; $ua->agent("Mozilla/5.0");

        but as I don't even know what LWP is, this is a pretty wild guess. might be another good read.


        John Davies

Re: Question regarding perl subroutine
by AnomalousMonk (Chancellor) on Jul 15, 2012 at 21:42 UTC
    my $ptf_filename; ... if ( $response->is_success ) { ... my $ptf_filename = $data->{PtfFileName}; ... } return $ptf_filename;

    There's another problem of masking lexical variable names similar to the one with the  $ua scalar. The first definition of  $ptf_filename before the if-block is masked by another definition of a lexical of the same name within the block. After the if-block, the value of the first lexical (which has never been given any value) is returned. (Technically, I guess this is not a masking problem but a scoping problem, as davies has alluded. It may – or may not – be perfectly valid semantically to define a lexical variable in one scope that has the same name as another lexical in a different scope. See Coping with Scoping.)

    Update: Had you been using warnings in the code posted originally, Perl would have warned you about the  $ua problem (this really is a masking problem).

    >perl -wMstrict -le "my $x = 1; my $x; print $x; " "my" variable $x masks earlier declaration in same scope at ... Use of uninitialized value $x in print at ...

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://981922]
Approved by davies
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (5)
As of 2018-05-27 11:05 GMT
Find Nodes?
    Voting Booth?