Actually I'm not sure I'm asking in the correct place, but I try my best. I have this snippet:
#!/bin/perl use strict; add_iLO3_data(); exit; sub add_iLO3_data() { $_ = "Memory | 0x0 | discrete | 0x4080| na + | na | na | na | na | na "; chomp; my @fields = split(/\|/); my $name = $fields[0]; $name =~ s/\s*//; $name =~ s/\s*$//; my $value = $fields[1]; $value =~ s/\s*//; $value =~ s/\s*$//; my $status = $fields[3]; $status =~ s/\s*//; $status =~ s/\s*$//; print "$name--$value--$status--\n"; # add to database my $id = get_sensor_id($name); my @variables = ("sensor_id", "OperationalStatus", "CurrentReading +"); my @readings = ($id, $value, $status); # here I add to DB } sub get_sensor_id($) { my $sensor = $_; print "sensor input: $sensor\n"; return 0; }
and I get as output:
Memory--0x0--0x4080-- sensor input: Memory | 0x0 | discrete | 0x4080| na + | na | na | na | na | na

The goal of the complete script is to parse an output and save some log data to a sqlite3 database, I cut out most of it.

I would expect to get in $sensor only "Memory", since I pass the variable $name by value (I tried by reference as well) and, as shown in the first sub, the variable $name seemed fine.

Where am I wrong?

Thank you very much!


In reply to Wrong content of variable passed to sub by olafmar

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.