Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re: sending data thru a sub routine

by AnomalousMonk (Archbishop)
on May 11, 2014 at 17:19 UTC ( [id://1085700]=note: print w/replies, xml ) Need Help??


in reply to sending data thru a sub routine

... when i run this script there will be two files that i will be run through this to extract and check data. What i really want to know is if i can use $ARGV[0] for both files if i run them through the subroutine one at a time.

I don't understand this. Do you mean that you will invoke the script twice, with a different file name given each time:
    system_prompt>perl your_script.pl file_1
    system_prompt>perl your_script.pl file_2
passing a single file name string to the  infile() subroutine on each script invocation? Or do you want to invoke the script with two file names at once:
    system_prompt>perl your_script.pl file_1 file_2
and process both files during one invocation of the script?

In the first case, two separate invocations of the script, using  $ARGV[0] for the file name is fine. In the second case, a single invocation with two file names, you need to realize that the two strings representing the file names will end up in  $ARGV[0] and  $ARGV[1] respectively, and you must process these two elements of the  @ARGV array independently and re-organiize the logic of your script accordingly.

Update: Finally realized that most Perl scripts are invoked not as
    script_name.pl parameter_1 param_2 ...
but as
    perl script_name.pl parameter_1 param_2 ...
and changed the command-line (pseudo-)code examples above accordingly.

Replies are listed 'Best First'.
Re^2: sending data thru a sub routine
by james28909 (Deacon) on May 11, 2014 at 19:15 UTC
    yes i will invoke this script or subroutine twice, once for each file. so....:
    system_prompt>my_script.pl $file1 system_prompt>my_script.pl $file2
    and i guess for each file i can do something like the following psuedocode to send it to the subroutine correct?
    my $file1 = 'extracted/file1; sub infile($file1); #will this be passed to ARGV[0]? #THEN further in the script... my $file2 = 'extracted/file2; sub infile(file2); #will this be sent to ARGV[0] as well after the fi +rst file?
      system_prompt>my_script.pl $file1 system_prompt>my_script.pl $file2

      If you're really doing this, with no intervening actions, and always having two files to process, you could instead do this:

      system_prompt>my_script.pl $file1 $file2

      And then, in my_script.pl:

      die "Usage: $0 file1 file2" unless @ARGV == 2; ... for (@ARGV) { process_file($_); } ... sub process_file { my ($filename) = @_; open my $input_fh, '<', $filename or die "Can't open '$filename': +$!"; ... }

      You have other issues in your code which you'll need to address. One that leapt out at me was this infinite loop:

      my $exit = ''; until ($exit) { ... code where $exit never becomes TRUE ... }

      You have two last statements but both are conditional on a pattern match. You should really have a bailout option, i.e. if you've done everything possible in the loop but are still looping, then die, warn and last or similar — and, instead of until ($exit) {...}, use while (1) {...} and get rid of the $exit variable altogether.

      sub infile($file1); #will this be passed to ARGV[0]? ... sub infile(file2); #will this be sent to ARGV[0] as well after the fi +rst file?

      Your (commented) questions about passing/sending to ARGV[0] [which should be $ARGV[0]] suggest you haven't really got a handle on the @ARGV array but, unfortunately, I don't know what you haven't understood. Take a look at "perlvar: Variables related to filehandles" and "perlop: I/O Operators". See what both of those sections say about @ARGV: that should either clarify the purpose and usage of @ARGV or, if not, provide you with the basis for more specific questions.

      -- Ken

        the last statement /can/ be one of many, and in my code you will see that i can add a last statement before or after formatting from hex to plain text, which is great that i could understand that, but on my observance in the files, it will always end with just the two that are in my code, if i come across a different "last" statement then i can easily add it in. the infinite loop would only happen if i didnt know what was in the file ;)
        im just trying to grasp the subroutine stuff :) i admit i am still pretty new to perl but it has been by far the best language to use so far.
        thanks for the ideas man, cheers :)
      I think that you are somewhat confused. You can do either of two (or possibly more) things: 1. Launch your script only once, with the two files as arguments, and process each argument one after the other with the same subroutine; or 2. launch the script twice, each time with only one argument. Both approaches are valid, it is up to you to decide how you want to do it, but I would personally tend to favor the first approach (this enables to take into account things that happened while reading the first file when reading the second one, which would be much more difficult with the second solution). The first solution could more or less look as follows:
      perl process_files.pl file1.txt file2.txt
      and, inside the program:
      for $inputfile (@ARGV) { process_file ($inputfile); }
      The second approach would probably require a shell script under Un*x, or *.bat command script under Windows (or *.com command file under VMS, or whatever with other OS's) to loop over the two filenames. One of the advantages of the first approach is that it can be more portable across platforms.
        but see, i wont know the file until i parse the main file. The file you looked at before in the other thread is part of another file, its a table within a table within a table.
        psuedocode: open main file for parsing; #not file1 or file2 parse out each file within this file; when reach $file1 send to subroutine; when reach $file2 send to subroutine; exit script;
        its hard to explain actually lol. All the files will be handled basically the same way as this subroutine above but will be a little bit different.
        The Main File has around 60 files inside of it. when you open the file in any hex editor... it has references just like the file i think you looked at a few days ago with file 1-25. the main thing with this script is will it allow me to go to $file1 and send it thru the sub routine, then go to $file2 and then send it thru the subroutine.

        TL;DR
        In essence these two files are within another file that i am parsing via hex offsets. i want to send each file thru one at a time in the same script.
        so i think this will work correct? :
        my $file1 = 'extracted/file1'; sub infile($file1); my $file2 = 'extracted/file2'; sub infile = 'extracted/file2';
Re^2: sending data thru a sub routine
by james28909 (Deacon) on May 14, 2014 at 02:01 UTC
    im using AS perl. so script.pl param1 param2 is just fine

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1085700]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (3)
As of 2024-04-19 22:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found