Results 1 to 3 of 3
  1. #1
    Join Date
    Feb 2007
    Posts
    5

    Unanswered: Problems returning value in subroutine

    Hi All

    I am rather frustrated with a subroutine that is supposed to does the following.

    Reads a date from a file
    removes the slashes
    return the new values

    This is the subroutine to do this
    sub ReadFile {
    $inputfilename = "$ENV{'PCRESCRIPT'}/config/effectivedate";
    open( INFILE, "< $inputfilename" ) or die "Can't open $inputfilename : $!";
    while( <INFILE> )
    {
    chomp;
    $line = $_;
    $line=~ s/\///g;
    $datval= $line;
    return "$datval\n";
    } close (INFILE); }

    In my second script, I would like to set a variable to this returned value. This is where I get stuck. This is how I am calling this routine in my second script.

    #!/usr/bin/perl
    require "/local5/depfa453/PCRESCRIPTS/batch/RUN/routinefile.pl";
    $PCREDATE=ReadFile($datval);

    Firstly, I hope I'm in the right direction but I get this error

    routinefile.pl did not return a true value at ./routinetest.pl line 2

    Any suggestions would be appreciated.

    Regards

  2. #2
    Join Date
    Feb 2006
    Posts
    56
    does it only return one line?

    Code:
    sub ReadFile {
       $inputfilename = "$ENV{'PCRESCRIPT'}/config/effectivedate";
       open( INFILE, "< $inputfilename" ) or die "Can't open $inputfilename : $!";
       while( <INFILE> )
       {
          chomp;
          $line = $_;
          $line=~ s/\///g;
       }
       close (INFILE); 
       return "$line\n";
      }
    }
    1;#<-- need this at the end routinefile.pl

    this line:

    $PCREDATE=ReadFile($datval);

    should just be:

    $PCREDATE=ReadFile();

    because you are not passing $datval to the sub routine, only getting a value back.

  3. #3
    Join Date
    Oct 2002
    Location
    Baghdad, Iraq
    Posts
    697
    (kevin covered it just fine, redundant comment removed.)
    Last edited by sco08y; 03-04-07 at 17:47.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •