Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

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= "v02k603.am.mot.com:9090/pcs/"; ############## 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 PtfSapGetSyncStg.pl syntax error at PtfSapGetSyncStg.pl line 164, near ") {" Global symbol "$ptf_filename" requires explicit package name at PtfSap +GetSyncStg.pl line 184. syntax error at PtfSapGetSyncStg.pl line 185, near "}" Execution of PtfSapGetSyncStg.pl 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

Comment on Question regarding perl subroutine
Select or Download Code
Re: Question regarding perl subroutine
by davies (Vicar) 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 PtfSapGetSyncStg.pl Can't call method "agent" on an undefined value at PtfSapGetSyncStg.pl + 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.

        Alexander

        --
        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 (http://perldoc.perl.org/perlintro.html#Variable-scoping) 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. http://search.cpan.org/~gaas/libwww-perl-6.04/lib/LWP/UserAgent.pm might be another good read.

        Regards,

        John Davies

Re: Question regarding perl subroutine
by AnomalousMonk (Monsignor) 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?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://981922]
Approved by davies
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (15)
As of 2014-07-28 15:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (204 votes), past polls