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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |