Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reading from strings or string-based file handles #1

Closed
nichtich opened this issue Jun 4, 2013 · 6 comments
Closed

Reading from strings or string-based file handles #1

nichtich opened this issue Jun 4, 2013 · 6 comments
Assignees

Comments

@nichtich
Copy link

nichtich commented Jun 4, 2013

I expected one of this to work:

open my $fh, '<', \$somestringwithbibtexdata;
Text::BibTeX::bibloop( sub { }, [$fh] );

Text::BibTeX::bibloop( sub { }, [\$somestringwithbibtexdata] );

Perl supports file handles based on string data since long ago.

@ghost ghost assigned plk Jun 4, 2013
@plk
Copy link
Collaborator

plk commented Jun 4, 2013

Yes, I also wished this worked. The problem is that the underlying C library libbtparse doesn't do PerlIO, only stdio since it wasn't written for Perl XS. Opening filehandles on strings is PerlIO only. I did briefly look at making libbtparse PerlIO compliant but it's not so easy.

@nichtich
Copy link
Author

nichtich commented Jun 4, 2013

One could at least let Text::BibTeX create a temporary file from the string and let libbtparse parse from it. This is a hack but users of a library should not have to care about the design of components two levels down the hierarchy of abstraction. Another solution is to split the string into BibTeX entries by regexp and feed each entry to Text::BibTeX::Entry.

@plk
Copy link
Collaborator

plk commented Jun 4, 2013

I agree that this isn't ideal - the problem is that the module was written (by someone else) for perl long before PerlIO even existed so nobody even thought of reading from strings like that. The temporary file this is a decent idea but needs careful though about where to create this across platforms (can't assume current dir is ok) and it means some fairly exciting XS hacking which I'm terrible at. For now, I'd do the temp file thing in your own code. That's what I had to do with writing STDERR from the module too - you can't write it to a variable for the same reason.

@ambs
Copy link
Owner

ambs commented Jun 4, 2013

Text::BibTeX::File open method is an interface with IO::File. Therefore, anything you can do with it should be possible to do with Text::BibTeX::File.
Given IO::File doesn't support (or doesn't seem to support) reading from a string, it is natural that doesn't work.

Nevertheless, I would prefer to have explicitly a Text::BibTeX::File->open_from_string or something.

@plk
Copy link
Collaborator

plk commented Jun 4, 2013

Actually, a dedicated string reading method would be best and follows the general interfaces for things like LibXML etc.

As I remember having fought with this too, I seem to recall that even though it uses IO::File in the Perl module, the C library is so old, it only uses very basic STDIO and this is the problem because the open methods in the module just pass directly to the library.

@nichtich
Copy link
Author

nichtich commented Jun 4, 2013

Given that normal lines in all BibTeX files I used so far never begin with @, that's what I use now:

foreach ( split /^\@/m, $string ) {
next unless $_;
my $entry = Text::BibTeX::Entry->new('@'.$_);
    ...
}

Anyway - the requirement to dig into this makes the whole library less useful for quick scripts. I better use another scripting language or another library next time :-(

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants