Evening! I'm trying to append to a scalar using a reference. I'm doing the same thing for an array which works, but this doesn't - isntead, it seems to have passed the reference name to my variable:
sub file_expansion { my ($output, $input) = @_; my $test = $output; # For testing whether I need to append + to an array or a scalar# while (<@$input>) { if (-e $_) { open (FILE, "< $_"); my @lines = <FILE>; foreach my $line (@lines) { $_ = $test; if (/SCALAR/) { $output .= $line; } else { push @$output, $line; } } } else { print "<<ERROR: File \'$_\' doesn't exist or c +an't be read.>>"; } } } @temp_fyle = "/home/monkey/list_of_commands"; (looks like below: interface blah ip address 1.2.3.4 255.255.255.255 ) &file_expansion(\$template,\@temp_fyle);
Ok, what I'd expect is that printing $output within the sub will result in the contents of the file being in there. What I'm finding, however, is that I get something like "SCALAR(0x828c810)interface blah ip address 1.2.3.4 255.255.255.255" And after exiting the loop, $template is empty. Obviously what's happening is that I've not referenced it properly and the $output variable actually has the scalar reference as its value rather than being a reference to the original scalar. What have I done wrong?
toeslikefingers.com - because you've got too much time on your hands

In reply to referencing question by bonoboy

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.