I am writing code that works with files, and I need to use the Input Record separator, $/. $/ is, obviously, set to a default of "\n". When I try to set it to another value, such as "ENDOFLINE" it stops working. That is when I try to print a single line it prints the entire file. It's as if it's take the value: undef . However, the interesting part is that when I print the actual VALUE of $/ it still prints ENDOFLINE

SUMMARY: 1) $/ has a value of "\n" and works perfectly UNTIL:
2) $/ is set to a new value. (ie "ENDOFLINE")
3) $/ appears to take the value of undef.
4) When $/ is read as a string, it appears to be correct! Ex: print "$/"; outputs the desired value: "ENDOFLINE" ALSO (just remembered) $/ functions as expected when it is set to a string of length 1 (such as "d" or "7")

I've asked on thescripts.com, and webdeveloper.com and nobody could find a problem with my code. I rewrote it, still didn't work. I took code from the blogs.perl.org tutorial that i learned perl from, and I experienced the same problem. My conclusion: $/ DOESN'T WORK!!! (?) Does anyone have any advice? My next step seems to be to uninstall and reinstall my perl engine. I thought I'd ask, first.

Thanks so much!

The code follows:

Well, here's the code. I figured that it hadn't helped so far, but you're right, rgiskard, i always helps.

Perl:

#!usr/bin/perl use warnings; use strict; open(XMLFILE, "students.xml"); $/ = "</student>"; for (<XMLFILE>){ print $_; print "ALALALALALALALA\n"; } close(XMLFILE);

students.xml: (in the same directory, of course)

<?xml version="1.0" encoding="UTF-8"?> <studentlist> <class id="Geography"> <student> <name>Lexi</name> <gender>F</gender> </student> <student> <name>Nelle</name> <gender>F</gender> </student> <student> <name>Josh</name> <gender>M</gender> </student> <student> <name>Jason</name> <gender>M</gender> </student> <student> <name>Ben</name> <gender>M</gender> </student> <student> <name>Larry</name> <gender>M</gender> </student> </class> <class id="English"> <student> <name>Caleb</name> <gender>M</gender> </student> <student> <name>Emily</name> <gender>F</gender> </student> <student> <name>Adelle</name> <gender>F</gender> </student> <student> <name>Mike</name> <gender>M</gender> </student> </class> </studentlist>

OUTPUT:

imac:~/begperl J$ perl abc.plx ??<?xml version="1.0" encoding="UTF-8"?> <studentlist> <class id="Geography"> <student> <name>Lexi</name> <gender>F</gender> </student> <student> <name>Nelle</name> <gender>F</gender> </student> <student> <name>Josh</name> <gender>M</gender> </student> <student> <name>Jason</name> <gender>M</gender> </student> <student> <name>Ben</name> <gender>M</gender> </student> <student> <name>Larry</name> <gender>M</gender> </student> </class> <class id="English"> <student> <name>Caleb</name> <gender>M</gender> </student> <student> <name>Emily</name> <gender>F</gender> </student> <student> <name>Adelle</name> <gender>F</gender> </student> <student> <name>Mike</name> <gender>M</gender> </student> </class> </studentlist>ALALALALALALALA imac:~/begperl J$

The "perl -v" command returns: This is perl, v5.8.6 built for darwin-thread-multi-2level


In reply to Input Record Separator WON'T WORK! by stalepretzel

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.