Actually I have to run the above code recursively to get the result: So here is my complete code:
#!/usr/bin/perl -w use diagnostics; use warnings; open $fh, "<", "$ARGV[0]" or die "Could not open $ARGV[0]: $!"; sub getsub{ my $sub = $_[0]; print "inside sub for $sub\n"; seek $fh, 0, 0; while (<$fh>) { if (/\.subckt $sub/../\.ends/) { print "starting from $_\n"; if (/^x/) { $line = $_; while (($nxt = readline($fh)) =~ /^\+/) { $line = $nxt; # print "line changed to $line\n"; } $line =~ s/\s+$//; # print "last line is $line\n"; my $sub = (split '\s', $line)[-1]; print "subcircuit found is $sub in $line\n"; my $pos = tell($fh); print "position in file set to $pos\n"; getsub ($sub); seek $fh, $pos, 0; $pos = tell ($fh); print "position in file restored to $pos\n"; print "line is now $_"; } } } } while (<$fh>) { if ($_ =~ /^xa1/) { $line = $_; print "line found to be $line\n"; while (($nxt = readline($fh)) =~ /^\+/) { $line = $nxt; #print "line changed to $line\n"; } $line =~ s/\s+$//; # print "last line is $line\n"; my $sub = (split '\s', $line)[-1]; print "subcircuit found is $sub in $line\n"; getsub($sub); } }
The file on which I am running the code is too big to be shared. The problem I am getting is inside the subroutine. What I want is whenever I return from the recursive call from the subroutine I want to start from the position where I left off in that routine. So I saved the position in $pos and used that in seek after the recursive call as suggested by you. When I return back from a recursive call I tried to check the line which I am on using the print statement at the end of the subroutine. But it shows $_ to be blank. Also, the if block starts from the beginning again creating an infinite loop. Why is this happening?

In reply to Re^2: Variable not set properly in perl by sar123
in thread Variable not set properly in perl by sar123

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.