Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Filehandle/array naming

by JSAWS (Novice)
on Nov 20, 2020 at 11:07 UTC ( [id://11123886]=perlquestion: print w/replies, xml ) Need Help??

JSAWS has asked for the wisdom of the Perl Monks concerning the following question:

been really struggling here.... probably a dumb approach

so I have 3 files, file1,file2,file3 generated by filehandles over ssh. File are written on local machine and contain CSV data coming from 6 nodes (need to stay like this). My script works but I am trying to optimize with a foreach loop on each of the files rather than hard coded for each.

so I have this array:
@FILES = ( 1, 2, 3 ); foreach $FILE (@FILES) { # Open filehandle tailing file written by ssh<br><br> open ( ${fh.$FILE}, "tail -f file$FILE |") || die "can't open file$ +FILE"; while ( ${line.$FILE} = ${fh.$FILE} ) { # Here I do my split on the CSV line and assign to arrays (keepi +ng the # $FILE value as a suffix to the array name)<br><br> @{fields.$FILE} = split(/,/,${line.$FILE}); push @{AAA.$FILE}, ${fields.$FILE[0]}; # .....

The issue I have is that ${line.$FILE} does not seem to be filled by the filehandle, I get the mem address (GLOB(0x563b358e1060) for example).

any help greatly appreciated!

Replies are listed 'Best First'.
Re: Filehandle/array naming
by bliako (Monsignor) on Nov 20, 2020 at 11:30 UTC

    try enclosing the filehandle in <>,

    Edit: The above won't work because the inside to the diamond operator must be a simple scalar variable and ${fh.$FILE} is not (why?). See choroba's answer

     while ( ${line.$FILE} = <${fh.$FILE}> ) {

    I must note that yours is definetely uncommon coding style! I usually use arrays rather than creating unqiue variables, one for each array item, whose name contains the array index. If I deciphered your code correctly that is.

    EDIT: I forgot the tail -f, perhaps you want to replace that with a Perl module: File::Tail

    bw, bliako

      This won't work, unfotunately. See perlop:

      > If what's within the angle brackets is neither a filehandle nor a simple scalar variable containing a filehandle name, typeglob, or typeglob reference, it is interpreted as a filename pattern to be globbed, and either a list of filenames or the next filename in the list is returned, depending on context. This distinction is determined on syntactic grounds alone. That means <$x> is always a readline() from an indirect handle, but <$hash{key}> is always a glob(). That's because $x is a simple scalar variable, but $hash{key} is not--it's a hash element. Even <$x > (note the extra space) is treated as glob("$x "), not readline($x).

      So, use readline, not the diamond.

      map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]

        A clarification is needed: if $x is a "simple scalar variable", then what is ${fh.$FILE}?

        Thx a Million Choroba, that solved it!!!!
      Thx Bliako, I did have the <> in my code, not sure why it did not make it in my post. But that did not work either, readline was the golden ticket!
Re: Filehandle/array naming
by Anonymous Monk on Nov 20, 2020 at 13:24 UTC
    open ( ${fh.$FILE}, "tail -f file$FILE |") || die "can't open file$FILE";
    You are probably much better off storing those filehandles in a data structure, say, a hash, instead of creating a set of related variables with programmatically obtained names. How can I use a variable as a variable name? explains it better than I can do at the moment.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (6)
As of 2024-03-28 08:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found