<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic perl sub routines in Operating System - Linux</title>
    <link>https://community.hpe.com/t5/operating-system-linux/perl-sub-routines/m-p/3858602#M98271</link>
    <description>Hi Everyone, &lt;BR /&gt;&lt;BR /&gt;I had a thread open the other day as I had a problem passing variables to a sub routine. Whilst this has been resolved, I now have a problem where I need to pass 3 variables, with the 3rd being a line of data. Such as&lt;BR /&gt;&lt;BR /&gt;default          10.100.2.248      UG        0   en0      -      -&lt;BR /&gt;&lt;BR /&gt;I get the line by parsing a file and creating a n variable with the following&lt;BR /&gt;&lt;BR /&gt;  s/(#.*|^MACHINE.*$|^.*CONFIG.* $type)//g;&lt;BR /&gt;&lt;BR /&gt;                chomp;&lt;BR /&gt;&lt;BR /&gt;                $data = $_;&lt;BR /&gt;&lt;BR /&gt;I have other variables that have been initialised &lt;BR /&gt;&lt;BR /&gt;I call my subroutine&lt;BR /&gt;&lt;BR /&gt;&amp;amp;insert_record(($hostid, $date, $data));&lt;BR /&gt;&lt;BR /&gt;The sub routine looks like the following&lt;BR /&gt;&lt;BR /&gt;my (@record)=(@_);&lt;BR /&gt;&lt;BR /&gt;my $stc;&lt;BR /&gt;&lt;BR /&gt;        $stc = $dbh-&amp;gt;prepare ("INSERT INTO tblConfig (hostid,date,config) VALUES(?,?,?)");&lt;BR /&gt;&lt;BR /&gt;        $stc-&amp;gt;execute ("$record[0],$record[1],$record[2]");&lt;BR /&gt;&lt;BR /&gt;        $stc-&amp;gt;finish ();&lt;BR /&gt;}&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;However, when I print the value of @record. I only get my first 2 values and not my line of data. &lt;BR /&gt;&lt;BR /&gt;I am currently looking at nested data structures, to see if I can do it that way&lt;BR /&gt;&lt;BR /&gt;Any help appreciated&lt;BR /&gt;&lt;BR /&gt;TIA&lt;BR /&gt;&lt;BR /&gt;Steve</description>
    <pubDate>Thu, 07 Sep 2006 10:27:45 GMT</pubDate>
    <dc:creator>steven Burgess_2</dc:creator>
    <dc:date>2006-09-07T10:27:45Z</dc:date>
    <item>
      <title>perl sub routines</title>
      <link>https://community.hpe.com/t5/operating-system-linux/perl-sub-routines/m-p/3858602#M98271</link>
      <description>Hi Everyone, &lt;BR /&gt;&lt;BR /&gt;I had a thread open the other day as I had a problem passing variables to a sub routine. Whilst this has been resolved, I now have a problem where I need to pass 3 variables, with the 3rd being a line of data. Such as&lt;BR /&gt;&lt;BR /&gt;default          10.100.2.248      UG        0   en0      -      -&lt;BR /&gt;&lt;BR /&gt;I get the line by parsing a file and creating a n variable with the following&lt;BR /&gt;&lt;BR /&gt;  s/(#.*|^MACHINE.*$|^.*CONFIG.* $type)//g;&lt;BR /&gt;&lt;BR /&gt;                chomp;&lt;BR /&gt;&lt;BR /&gt;                $data = $_;&lt;BR /&gt;&lt;BR /&gt;I have other variables that have been initialised &lt;BR /&gt;&lt;BR /&gt;I call my subroutine&lt;BR /&gt;&lt;BR /&gt;&amp;amp;insert_record(($hostid, $date, $data));&lt;BR /&gt;&lt;BR /&gt;The sub routine looks like the following&lt;BR /&gt;&lt;BR /&gt;my (@record)=(@_);&lt;BR /&gt;&lt;BR /&gt;my $stc;&lt;BR /&gt;&lt;BR /&gt;        $stc = $dbh-&amp;gt;prepare ("INSERT INTO tblConfig (hostid,date,config) VALUES(?,?,?)");&lt;BR /&gt;&lt;BR /&gt;        $stc-&amp;gt;execute ("$record[0],$record[1],$record[2]");&lt;BR /&gt;&lt;BR /&gt;        $stc-&amp;gt;finish ();&lt;BR /&gt;}&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;However, when I print the value of @record. I only get my first 2 values and not my line of data. &lt;BR /&gt;&lt;BR /&gt;I am currently looking at nested data structures, to see if I can do it that way&lt;BR /&gt;&lt;BR /&gt;Any help appreciated&lt;BR /&gt;&lt;BR /&gt;TIA&lt;BR /&gt;&lt;BR /&gt;Steve</description>
      <pubDate>Thu, 07 Sep 2006 10:27:45 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/perl-sub-routines/m-p/3858602#M98271</guid>
      <dc:creator>steven Burgess_2</dc:creator>
      <dc:date>2006-09-07T10:27:45Z</dc:date>
    </item>
    <item>
      <title>Re: perl sub routines</title>
      <link>https://community.hpe.com/t5/operating-system-linux/perl-sub-routines/m-p/3858603#M98272</link>
      <description>Hi Steve:&lt;BR /&gt;&lt;BR /&gt;Run this, and ask yourself what is returned by the substitution:&lt;BR /&gt;&lt;BR /&gt;# perl -wnle 's/(#.*|^MACHINE.*$|^.*CONFIG.* $type)//g;chomp;print "[ $1 ] [$_] \n"'&lt;BR /&gt;&lt;BR /&gt;That is, enter things like:&lt;BR /&gt;&lt;BR /&gt;MACHINE123&lt;BR /&gt;nomore&lt;BR /&gt;MACHINE&lt;BR /&gt;&lt;BR /&gt;...and observe where the captured/non-captured output is returned...&lt;BR /&gt;&lt;BR /&gt;Regards!&lt;BR /&gt;&lt;BR /&gt;...JRF...</description>
      <pubDate>Thu, 07 Sep 2006 10:55:16 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/perl-sub-routines/m-p/3858603#M98272</guid>
      <dc:creator>James R. Ferguson</dc:creator>
      <dc:date>2006-09-07T10:55:16Z</dc:date>
    </item>
    <item>
      <title>Re: perl sub routines</title>
      <link>https://community.hpe.com/t5/operating-system-linux/perl-sub-routines/m-p/3858604#M98273</link>
      <description>I read that post after you got the `fix'&lt;BR /&gt;&lt;BR /&gt;First of all, some things are done overly complicated, as context matters in perl:&lt;BR /&gt;&lt;BR /&gt;my (@list) = (@other_list);&lt;BR /&gt;&lt;BR /&gt;is only adding line noise, as both sides are list context&lt;BR /&gt;&lt;BR /&gt;Using leading &amp;amp; for function calls is severely discouraged, as it has unwanted side effects. This still exists for some backward compatability with perl4 and the possibility to deep-optimize sub calls, something you will probably not be doing in the next 5 years :)&lt;BR /&gt;&lt;BR /&gt;The execute statement is wrong, as you pass all arguments to it as one single string&lt;BR /&gt;&lt;BR /&gt;--8&amp;lt;---&lt;BR /&gt;sub db_insert&lt;BR /&gt;{&lt;BR /&gt;    my ($hostid, $date, $config) = @_;&lt;BR /&gt;&lt;BR /&gt;    my $stc = $dbh-&amp;gt;prepare ("INSERT INTO tblConfig (hostid, date, config) VALUES (?, ?, ?)");&lt;BR /&gt;    $stc-&amp;gt;execute ($hostid, $date, $config);&lt;BR /&gt;    $stc-&amp;gt;finish ();&lt;BR /&gt;    } # db_insert&lt;BR /&gt;&lt;BR /&gt;Would be the right thing to do, but ...&lt;BR /&gt;&lt;BR /&gt;1. The finish is optional. (I also always use it for clarity, but still)&lt;BR /&gt;2. The prepare is done *inside* the routine, causing it to be prepared over and over and over again. That is bad!&lt;BR /&gt;&lt;BR /&gt;BTW, chomp and assign can go in one statement, which would make your simplified flow look like&lt;BR /&gt;&lt;BR /&gt;---8&amp;lt;---&lt;BR /&gt;use strict;&lt;BR /&gt;use warnings;&lt;BR /&gt;use DBI;&lt;BR /&gt;&lt;BR /&gt;my $dbh = DBI-&amp;gt;connect (....);&lt;BR /&gt;my $sti = $dbh-&amp;gt;prepare ("INSERT INTO tblConfig (hostid, date, config) VALUES (?, ?, ?)");&lt;BR /&gt;&lt;BR /&gt;sub db_insert&lt;BR /&gt;{&lt;BR /&gt;    my ($hostid, $date, $config) = @_;&lt;BR /&gt;&lt;BR /&gt;    $sti-&amp;gt;execute ($hostid, $date, $config);&lt;BR /&gt;    } # db_insert&lt;BR /&gt;&lt;BR /&gt;my $hostid = 12345;&lt;BR /&gt;my $date   = 20060907;&lt;BR /&gt;&lt;BR /&gt;while (&amp;lt;&amp;gt;) {&lt;BR /&gt;    s/(#.*|^MACHINE.*$|^.*CONFIG.* $type)//g;&lt;BR /&gt;    chomp ($data = $_);&lt;BR /&gt;    db_insert ($hostid, $date, $data);&lt;BR /&gt;    }&lt;BR /&gt;&lt;BR /&gt;$sti-&amp;gt;finish ();&lt;BR /&gt;$dbh-&amp;gt;commit;&lt;BR /&gt;--&amp;gt;8---&lt;BR /&gt;&lt;BR /&gt;And, as db_insert as sub now only has one line, why not move that line to inside the while loop and get rid of the sub.&lt;BR /&gt;&lt;BR /&gt;Enjoy, Have FUN! H.Merijn</description>
      <pubDate>Thu, 07 Sep 2006 11:06:03 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/perl-sub-routines/m-p/3858604#M98273</guid>
      <dc:creator>H.Merijn Brand (procura</dc:creator>
      <dc:date>2006-09-07T11:06:03Z</dc:date>
    </item>
    <item>
      <title>Re: perl sub routines</title>
      <link>https://community.hpe.com/t5/operating-system-linux/perl-sub-routines/m-p/3858605#M98274</link>
      <description>&lt;!--!*#--&gt;I read that post after you got the `fix'&lt;BR /&gt;&lt;BR /&gt;First of all, some things are done overly complicated, as context matters in perl:&lt;BR /&gt;&lt;BR /&gt;my (@list) = (@other_list);&lt;BR /&gt;&lt;BR /&gt;is only adding line noise, as both sides are list context&lt;BR /&gt;&lt;BR /&gt;Using leading &amp;amp; for function calls is severely discouraged, as it has unwanted side effects. This still exists for some backward compatability with perl4 and the possibility to deep-optimize sub calls, something you will probably not be doing in the next 5 years :)&lt;BR /&gt;&lt;BR /&gt;The execute statement is wrong, as you pass all arguments to it as one single string&lt;BR /&gt;&lt;BR /&gt;--8&amp;lt;---&lt;BR /&gt;sub db_insert&lt;BR /&gt;{&lt;BR /&gt;    my ($hostid, $date, $config) = @_;&lt;BR /&gt;&lt;BR /&gt;    my $stc = $dbh-&amp;gt;prepare ("INSERT INTO tblConfig (hostid, date, config) VALUES (?, ?, ?)");&lt;BR /&gt;    $stc-&amp;gt;execute ($hostid, $date, $config);&lt;BR /&gt;    $stc-&amp;gt;finish ();&lt;BR /&gt;    } # db_insert&lt;BR /&gt;&lt;BR /&gt;Would be the right thing to do, but ...&lt;BR /&gt;&lt;BR /&gt;1. The finish is optional. (I also always use it for clarity, but still)&lt;BR /&gt;2. The prepare is done *inside* the routine, causing it to be prepared over and over and over again. That is bad!&lt;BR /&gt;&lt;BR /&gt;BTW, chomp and assign can go in one statement, which would make your simplified flow look like&lt;BR /&gt;&lt;BR /&gt;---8&amp;lt;---&lt;BR /&gt;use strict;&lt;BR /&gt;use warnings;&lt;BR /&gt;use DBI;&lt;BR /&gt;&lt;BR /&gt;my $dbh = DBI-&amp;gt;connect (....);&lt;BR /&gt;my $sti = $dbh-&amp;gt;prepare ("INSERT INTO tblConfig (hostid, date, config) VALUES (?, ?, ?)");&lt;BR /&gt;&lt;BR /&gt;sub db_insert&lt;BR /&gt;{&lt;BR /&gt;    my ($hostid, $date, $config) = @_;&lt;BR /&gt;&lt;BR /&gt;    $sti-&amp;gt;execute ($hostid, $date, $config);&lt;BR /&gt;    } # db_insert&lt;BR /&gt;&lt;BR /&gt;my $hostid = 12345;&lt;BR /&gt;my $date   = 20060907;&lt;BR /&gt;&lt;BR /&gt;while (&amp;lt;&amp;gt;) {&lt;BR /&gt;    s/(#.*|^MACHINE.*$|^.*CONFIG.* $type)//g;&lt;BR /&gt;    chomp ($data = $_);&lt;BR /&gt;    db_insert ($hostid, $date, $data);&lt;BR /&gt;    }&lt;BR /&gt;&lt;BR /&gt;$sti-&amp;gt;finish ();&lt;BR /&gt;$dbh-&amp;gt;commit;&lt;BR /&gt;--&amp;gt;8---&lt;BR /&gt;&lt;BR /&gt;And, as db_insert as sub now only has one line, why not move that line to inside the while loop and get rid of the sub.&lt;BR /&gt;&lt;BR /&gt;Enjoy, Have FUN! H.Merijn</description>
      <pubDate>Thu, 07 Sep 2006 11:06:25 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/perl-sub-routines/m-p/3858605#M98274</guid>
      <dc:creator>H.Merijn Brand (procura</dc:creator>
      <dc:date>2006-09-07T11:06:25Z</dc:date>
    </item>
    <item>
      <title>Re: perl sub routines</title>
      <link>https://community.hpe.com/t5/operating-system-linux/perl-sub-routines/m-p/3858606#M98275</link>
      <description>Hi Guys&lt;BR /&gt;&lt;BR /&gt;Thanks for the replies. Reading over the responses highlights that I need to take perl from the beginning and not try to dive in without fully understanding what is being done and why. &lt;BR /&gt;&lt;BR /&gt;I am trying to learn a whole lot in a relatively short space of time. I need to get my Msc assignment handed in by monday and have bitten off more than I can chew !!&lt;BR /&gt;&lt;BR /&gt;Thanks again&lt;BR /&gt;&lt;BR /&gt;Probably more to come&lt;BR /&gt;&lt;BR /&gt;Steve</description>
      <pubDate>Fri, 08 Sep 2006 02:44:08 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/perl-sub-routines/m-p/3858606#M98275</guid>
      <dc:creator>steven Burgess_2</dc:creator>
      <dc:date>2006-09-08T02:44:08Z</dc:date>
    </item>
  </channel>
</rss>

